package com.cfec.educationplatform.Controller;

import com.cfec.educationplatform.domain.TbTopic;
import com.cfec.educationplatform.service.TbTopicService;
import com.cfec.educationplatform.service.impl.TbTopicServiceImpl;
import com.cfec.educationplatform.util.ResultData;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

@RestController
@RequestMapping("/camera")
@ApiOperation("拍题")
@Slf4j
public class CameraController {

    @Resource(name = "tbTopicServiceImpl")
    TbTopicServiceImpl tbTopicService;

    @Value("D/img/edpl")
    private String filePath;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public ResultData<Object> fileUpload(@RequestParam("file") MultipartFile file) {
        // 判断文件是否为空
        if (file == null) {
            return new ResultData<>(400, "参数文件为空", null);
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        System.out.println("文件名:" + file.getOriginalFilename());
        System.out.println("文件大小:" + file.getSize() + "字节");
        System.out.println("文件类型:" + file.getContentType());

        //重新生成的文件名
        String saveFileName = file.getOriginalFilename();
        File directory = new File(filePath);
        if (!directory.exists()) {
            directory.mkdir();
        }
        //在指定目录下创建该文件
        File targetFile = new File(directory, saveFileName);
        try {
            file.transferTo(targetFile);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return new ResultData<>(200, "上传成功,返回图片地址", "/img/" + saveFileName);
    }

    @PostMapping("/up_camera")
    @ApiOperation("上传拍题")
    public ResultData<Object> upCamera(@RequestBody TbTopic tbTopic){
        log.info("上传拍题：{}",tbTopic);
        return tbTopicService.upCameraTopic(tbTopic);
    }


}
